package com.colbby.datahouse.db.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;

public class ResultSetIterator implements Iterator<Map<String, Object>> {

	private ResultSet rs = null;
	private boolean nexted = false;
	
	public ResultSetIterator(ResultSet rs) {
		this.rs = rs;
	}
	
	@Override
	public boolean hasNext() {
		if(!nexted) {
			try {
				nexted = true;
				return rs.next();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return true;
	}

	@Override
	public Map<String, Object> next() {
		if(hasNext()) {
			try {
				Map<String, Object> map = new ResultSetMetaDataMap(rs.getMetaData(), rs);
				this.nexted = false;
				return map;
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		} else {
			throw new NoSuchElementException();
		}
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}
}
